package com.example.module_parking.adapter


import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.example.module_parking.R
import com.example.module_parking.model.ParkingEntity
import com.google.gson.Gson


class ParkingAdapter :RecyclerView.Adapter<ParkingAdapter.ParkingViewHolder>() {
    var pakingList: ArrayList<ParkingEntity> = arrayListOf()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParkingViewHolder {
        return ParkingViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_parking, parent, false)
        )
    }

    override fun getItemCount(): Int {
        return pakingList.size
    }

    fun addParkingList(list: List<ParkingEntity>) {
        pakingList.clear()
        pakingList.addAll(list)
        notifyDataSetChanged()
    }
    fun addSearchParking(entity:ParkingEntity) {
        pakingList.clear()
        pakingList.add(entity)
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: ParkingViewHolder, position: Int) {
        holder.type.text = pakingList[position].spaceType
        holder.spacenum.text = pakingList[position].spaceNumber
        holder.area_name.text = pakingList[position].areaName
        holder.status.text = pakingList[position].spaceStatus


        //车位状态（空闲、已占用、维修中 通过字典表配置）
        when (pakingList[position].spaceStatus) {
            "空闲" -> {
                holder.status.setBackgroundResource(R.drawable.bg_green)
                holder.status.setTextColor(Color.parseColor("#50BA76"))
            }
            "已占用" -> {
                holder.status.setBackgroundResource(R.drawable.bg_blue)
                holder.status.setTextColor(Color.parseColor("#2563EB"))
            }
            "维修中" -> {
                holder.status.setBackgroundResource(R.drawable.bg_yellow)
                holder.status.setTextColor(Color.parseColor("#CA8A04"))
            }
        }

        holder.itemView.setOnClickListener {
            val json = Gson().toJson(pakingList[position])
            ARouter.getInstance().build("/module_parking/EditParkingActivity")
                .withString("entity",json)
                .navigation()

        }

    }


   inner class ParkingViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val type: TextView = itemView.findViewById(R.id.type)
        val spacenum: TextView = itemView.findViewById(R.id.spacenum)
        val area_name: TextView = itemView.findViewById(R.id.area_name)
        val status: TextView = itemView.findViewById(R.id.status)

    }

}







